package com.sc.nft.dao;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.entity.vo.AdminPageVigorousRecordVO;
import com.sc.nft.enums.VigorousStatusEnum;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.UserVigorousRecords;
import com.sc.nft.mapper.UserVigorousRecordsMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 质押任务-用户激活记录(sc_user_vigorous_records)数据DAO
 *
 * @author kancy
 * @since 2023-06-13 14:55:35
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class UserVigorousRecordsDao extends ServiceImpl<UserVigorousRecordsMapper, UserVigorousRecords> {

    public Page<AdminPageVigorousRecordVO> adminPageVigorousRecord(Long pledgeTaskId,String userTel, Integer pageNo, Integer pageSize) {
        return baseMapper.adminPageVigorousRecord(new Page<>(pageNo, pageSize), pledgeTaskId,userTel);
    }

    public Integer getOneByUserIdAndPledgeTaskIdAndVigorousStatus(Long userId, Long pledgeTaskId, VigorousStatusEnum vigorousStatus) {
        LambdaQueryWrapper<UserVigorousRecords> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserVigorousRecords::getUserId, userId);
        wrapper.eq(UserVigorousRecords::getPledgeTaskId, pledgeTaskId);
        wrapper.eq(UserVigorousRecords::getVigorousStatus, vigorousStatus);
        return count(wrapper);
    }

    public UserVigorousRecords getOneByPledgeTaskIdAndUserId(Long userId, Long pledgeTaskId, VigorousStatusEnum vigorousStatus) {
        LambdaQueryWrapper<UserVigorousRecords> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserVigorousRecords::getUserId, userId);
        wrapper.eq(UserVigorousRecords::getPledgeTaskId, pledgeTaskId);
        wrapper.eq(ObjectUtil.isNotNull(vigorousStatus),UserVigorousRecords::getVigorousStatus, vigorousStatus);
        wrapper.last("order by id desc limit 1");
        return getOne(wrapper);
    }

    public Boolean updateVigorousStatusById(Long id, VigorousStatusEnum vigorousStatusEnum) {
        UpdateWrapper<UserVigorousRecords> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.eq("vigorous_status", VigorousStatusEnum.ACTIVATED.getCode());
        wrapper.set("vigorous_status", vigorousStatusEnum.getCode());
        return update(new UserVigorousRecords(),wrapper);
    }

    public Boolean updateVigorousCountById(Long id, Integer vigorousCount) {
        UpdateWrapper<UserVigorousRecords> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("vigorous_count", vigorousCount);
        return update(new UserVigorousRecords(),wrapper);
    }

    public UserVigorousRecords getOneByPledgeTaskIdAndUserIdAndId(Long userId, Long pledgeTaskId, Long id) {
        LambdaQueryWrapper<UserVigorousRecords> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserVigorousRecords::getUserId, userId);
        wrapper.eq(UserVigorousRecords::getPledgeTaskId, pledgeTaskId);
        wrapper.eq(UserVigorousRecords::getId, id);
        return getOne(wrapper);
    }
}